<?php

namespace App\Http\Controllers\Admin\Scene\Service;

use App\Exceptions\WechatException;
use App\Http\Controllers\Admin\Scene\Repository\Spot as SpotRepository;
use App\Repositories\PageRepository;
use App\Service\WechatPageService;
use Illuminate\Http\Request;

class Spot
{
    /**
     * 发布状态 1启用 2禁用
     */
    const ON = 1;
    const OFF = 2;


    /**
     * Http请求
     * @var $request
     */
    private $request;
    private $repository;
    protected $wechatPageService;

    public function __construct ()
    {
        $this->repository = new SpotRepository();
        $this->pageRepository=new PageRepository();
    }
    //常用参数操作
    /**
     * @param $status
     *
     * @return mixed
     */
    public function getStatusString($status){
        $statusList =  $this->getStatusList();
        return $statusList[$status];
    }


    /**
     * @return array
     */
    public static function getStatusList(){
        return [
            self::ON =>  '启用',
            self::OFF =>  '禁用'
        ];
    }



    //读操作



    public function getSpot($id){
        return $this->repository->find($id);
    }
    public static function getSelectList(){
        return SpotRepository::where('status',self::ON)->get();
    }
    /**
     * @param Request $request
     *
     * @return $this|\Illuminate\Database\Eloquent\Builder|static
     */
    public function getPageList(Request $request,$order=false){
        $this->request = $request;
        $model=$this->repository;
        //关键字
        if(isset($this->request->searchKey)){
            $model=$model->where('title','like', '%'.$this->request->searchKey.'%');
        }

        //状态
        if(isset($this->request->status)&&$this->request->status!=''){
            $model=$model->where('status','=',$this->request->status);
        }
        //时间
        if(isset($this->request->searchTime)&&$this->request->searchTime!=''){
            $timeArray=explode('~',$this->request->searchTime);
            $start_time=trim($timeArray[0]);
            $end_time=trim($timeArray[1]);
            $model=$model->whereBetween('created_at',[$start_time,$end_time]);
        }
        if($order==true){
            $model = $model->orderBy('sort','ASC');
        }
        $model=$model->paginate($this->request->limit);

        if($model){
            $model=$model->toArray();
        }
        foreach ($model['data'] as $k=>&$v){
            $v['status_name'] = self::getStatusList()[$v['status']];
        }
        return $model;
    }

    //写操作
    /**
     * 禁用
     * @param array $id
     */
    public function draft($id = []){
        if($id == []){
            $this->repository->where('status',self::ON)->update(['status'=>self::OFF]);
        }else{
            if(is_array($id)){
                $this->repository->whereIn('id',$id)->where('status',self::ON)->update(['status'=>self::OFF]);
            }else{
                $this->repository->where('id',$id)->where('status',self::ON)->update(['status'=>self::OFF]);

            }
        }
    }

    /**
     * 启用
     * @param array $id
     */
    public function published($id = []){
        if($id == []){
            $this->repository->where('status',self::OFF)->update(['status'=>self::ON]);
        }else{
            if(is_array($id)){
                $this->repository->whereIn('id',$id)->where('status',self::OFF)->update(['status'=>self::ON]);
            }else{
                $this->repository->where('id',$id)->where('status',self::OFF)->update(['status'=>self::ON]);

            }
        }
    }


    public function sortCol($id = [],$sort = 50){
        if($id == []){
        }else{
            if(is_array($id)){
                $this->repository->whereIn('id',$id)->where('sort','<>',$sort)->update(['sort'=>$sort]);
            }else{
                $this->repository->where('id',$id)->where('sort','<>',$sort)->update(['sort'=>$sort]);

            }
        }
    }

    /**
     * @param array $id
     */
    public function del($id = []){
            if($id == []){

            }else{
                if(is_array($id)){
                    foreach ($id as $k=>$v){
                        $this->pageRepository->delete($v);
                    }
                    $this->repository->whereIn('id',$id)->delete();
                }else{
                    $this->pageRepository->delete($id);

                    $this->repository->where('id',$id)->delete();

                }
            }
    }

    /**
     * @param $post
     */
    public function insertOrUpdate($post){
        if(isset($post['id'])){
            $model = $this->repository->find($post['id']);
            //修改page

        }else{
            $model = $this->repository;
            //增加page
        }
        $model->title = $post['title'];
        $model->voice = $post['voice'];
        $model->thumb = $post['thumb'];
        $model->desc = $post['desc'];
        $model->content = $post['content'];
        $model->sort = $post['sort']||$post['sort']===0?$post['sort']:50;
        $model->status = isset($post['status'])?$post['status']:self::OFF;
        $model->save();
        if(isset($post['id'])){
            //修改页面
            if($model->page_id){
                $this->pageRepository->updatePage($post,$model);
            }else{
                $page=$this->pageRepository->addPage($post,$model->id);
                if($page['errcode']==0){
                    $model->page_id=$page['data']['page_id'];
                    $model->save();
                }else{
                    throw new WechatException($page['errcode'],$page['errmsg']);
                }
            }
        }else{
            $page=$this->pageRepository->addPage($post,$model->id);
            if($page['errcode']==0){
                $model->page_id=$page['data']['page_id'];
                $model->save();
            }else{
                abort($page['errcode'],$page['errmsg']);
            }
        }
    }
}
